﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Diagnostics;
using UygulamaTakbisService.wsTakbisServis;
using UygulamaTakbisService.Classes;
using System.Data;


namespace UygulamaTakbisService
{
    public class LogDao : IDisposable
    {
        TAKBISWebService takbisServis = new TAKBISWebService();

        private XmlDocument CreateXML(Object classObject)
        {
            XmlDocument xmlDoc = null;
            try
            {
                xmlDoc = new XmlDocument();
                XmlSerializer xmlSerializer = new XmlSerializer(classObject.GetType());
                using (MemoryStream xmlStream = new MemoryStream())
                {
                    xmlSerializer.Serialize(xmlStream, classObject);
                    xmlStream.Position = 0;
                    xmlDoc.Load(xmlStream);
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
            }
            return xmlDoc;
        }

        private Object CreateObject(string XMLString, Object classObject)
        {
            XmlSerializer oXmlSerializer = new XmlSerializer(classObject.GetType());
            classObject = oXmlSerializer.Deserialize(new StringReader(XMLString));
            return classObject;
        }

        private bool TakbisBilgiSorguLoguKaydet(string metotAdi, XmlDocument gelenXml, XmlDocument gidenXml, string gelenParametre, decimal tkgmWebServisCevapSuresi, decimal TkgmWebServisGelenObjeKayitSuresi, long kullaniciRef)
        {
            bool sonuc = false;
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TakbisBilgiSorguLogConnection"].ToString()))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    con.Open();
                    SqlTransaction transaction = con.BeginTransaction("trans");
                    try
                    {
                        cmd.CommandText = @"insert into IslemLog with(updlock) (MetotAdi,GelenXml,GidenXml,KayitTarih,GelenParametre,TkgmWebServisCevapSuresi,TkgmWebServisGelenObjeKayitSuresi,KullaniciRef) values (@MetotAdi,@GelenXml,@GidenXml,getdate(),@GelenParametre,@TkgmWebServisCevapSuresi,@TkgmWebServisGelenObjeKayitSuresi,@Kullaniciref)";
                        cmd.Parameters.Add("@MetotAdi", metotAdi);

                        if (gelenXml != null)
                            cmd.Parameters.Add("@GelenXml", gelenXml.InnerXml);
                        else
                            cmd.Parameters.Add("@GelenXml", DBNull.Value);

                        if (gidenXml != null)
                            cmd.Parameters.Add("@GidenXml", gidenXml.InnerXml);
                        else
                            cmd.Parameters.Add("@GidenXml", DBNull.Value);

                        if (gelenParametre != null)
                            cmd.Parameters.Add("@GelenParametre", gelenParametre);
                        else
                            cmd.Parameters.Add("@GelenParametre", DBNull.Value);

                        cmd.Parameters.Add("@TkgmWebServisGelenObjeKayitSuresi", TkgmWebServisGelenObjeKayitSuresi);
                        cmd.Parameters.Add("@TkgmWebServisCevapSuresi", tkgmWebServisCevapSuresi);
                        cmd.Parameters.Add("@Kullaniciref", kullaniciRef);

                        cmd.Connection = con;
                        cmd.Transaction = transaction;
                        cmd.ExecuteScalar();
                        transaction.Commit();
                        sonuc = true;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        using (ExceptionLogDao el = new ExceptionLogDao())
                        {
                            el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                        }
                    }
                    finally
                    {
                        con.Close();
                        con.Dispose();
                        cmd.Dispose();
                    }
                    return sonuc;
                }
            }
        }

        public bool TakbisBilgiSorguLogKaydet(string metotAdi, object gelenObject, object gidenObject, string gelenParametre, decimal tkgmWebServisCevapSuresi, decimal tkgmWebServisGelenObjeKayitSuresi, long kullaniciRef)
        {
            XmlDocument xmlGelenDoc = null;
            XmlDocument xmlGidenDoc = null;

            if (gelenObject != null)
                xmlGelenDoc = CreateXML(gelenObject);

            if (gidenObject != null)
                xmlGidenDoc = CreateXML(gidenObject);

            return TakbisBilgiSorguLoguKaydet(metotAdi, xmlGelenDoc, xmlGidenDoc, gelenParametre, tkgmWebServisCevapSuresi, tkgmWebServisGelenObjeKayitSuresi, kullaniciRef);
        }

        public List<TakbisBilgiSorguLog> GetirTakbisBilgiSorguLog(int baslangicgun, int baslangicay, int baslangicyil, int bitisgun, int bitisay, int bitisyil, Int64 sorgulananKullanici, Int64 kullaniciID)
        {
            List<TakbisBilgiSorguLog> takbisBilgiSorguLogList = null;
            try
            {
                using (LogDao logDao = new LogDao())
                {
                    string baslangicTarih = baslangicay.ToString() + "/" + baslangicgun.ToString() + "/" + baslangicyil.ToString();
                    string bitisTarih = bitisay.ToString() + "/" + bitisgun.ToString() + "/" + bitisyil.ToString();
                    takbisBilgiSorguLogList = TakbisBilgiSorguLogGetir(kullaniciID, baslangicTarih, bitisTarih);
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
            }
            return takbisBilgiSorguLogList;
        }

        public List<TakbisBilgiSorguLog> TakbisBilgiSorguLogGetir(Int64 sorgulananKullanici, string baslamatarih, string bitisTarih)
        {
            List<TakbisBilgiSorguLog> TakbisBilgiSorguLogList = null;
            try
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TakbisBilgiSorguLogConnection"].ToString()))
                {
                    string sql =    "select ID," +
                    "       MetotAdi, " +
                    "       GelenParametre as GidenParametre, " +
                    "       GelenXml as GidenObje, " +
                    "       GidenXml as CevapObjesi, " +
                    "       TkgmWebServisCevapSuresi, " +
                    "       TkgmWebServisGelenObjeKayitSuresi, " +
                    "       KayitTarih " +
                    "from IslemLog with(nolock) " +
                    "where " +
                    "      KullaniciRef = " + sorgulananKullanici.ToString() + " and " +
                    "      KayitTarih>='" + baslamatarih + " 00:01' and " +
                    "      KayitTarih<='" + bitisTarih + " 23:59' " +
                    "order by KayitTarih Desc";
                    using (SqlCommand cmd = new SqlCommand(sql))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            cmd.CommandTimeout = 60000;
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            if (dt.Rows.Count > 0)
                            {
                                TakbisBilgiSorguLogList = new List<TakbisBilgiSorguLog>();
                                foreach (DataRow dr in dt.Rows)
                                {
                                    TakbisBilgiSorguLog takbisBilgiSorguLog = new TakbisBilgiSorguLog(dr);
                                    TakbisBilgiSorguLogList.Add(takbisBilgiSorguLog);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                using (ExceptionLogDao el = new ExceptionLogDao())
                {
                    el.HataLogYaz(DateTime.Now.ToString() + "  " + ex.ToString());
                }
            }
            return TakbisBilgiSorguLogList;
        }

        public void Dispose()
        {
        }
    }
}
